package com.guigu.work.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.guigu.base.entity.vo.TransportOrderDashboardVO;
import com.guigu.tms.exception.TmsException;
import com.guigu.tms.result.R;
import com.guigu.tms.result.ResultCode;
import com.guigu.work.dto.TransportOrderDTO;
import com.guigu.work.entity.TransportOrder;
import com.guigu.work.feign.BaseFeign;
import com.guigu.work.feign.DispatchFeign;
import com.guigu.work.service.TransportOrderService;
import com.guigu.work.vo.TransportOrderDetailsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 运单表 前端控制器
 * </p>
 *
 * @author liAo
 * @since 2021-11-09
 */
@Api(tags = "运单操作接口")
@RestController
@RequestMapping("/transport-order")
public class TransportOrderController {
    private TransportOrderService service;
    private BaseFeign baseFeign;
    private DispatchFeign dispatchFeign;

    public TransportOrderController(TransportOrderService service, BaseFeign baseFeign, DispatchFeign dispatchFeign) {
        this.service = service;
        this.baseFeign = baseFeign;
        this.dispatchFeign = dispatchFeign;
    }

    /**
     * 新增运单
     *
     * @param dto 运单信息
     * @return 运单信息
     */
    @ApiOperation("新增运单")
    @PostMapping("/save")
    public R<TransportOrder> save(@RequestBody TransportOrderDTO dto) {
        TransportOrder transportOrder = service.saveTransportOrder(dto);

        return new R(ResultCode.SUCCESS_CODE, "添加运单信息成功!", transportOrder);
    }

    /**
     * 修改运单
     *
     * @param dto 运单信息
     * @return 运单信息
     */
    @ApiOperation("修改运单")
    @PostMapping("/update")
    public R<TransportOrder> update(@RequestBody TransportOrderDTO dto) {
        TransportOrder transportOrder = service.updateTransportOrder(dto);

        return new R(ResultCode.SUCCESS_CODE, "修改运单信息成功!", transportOrder);
    }

    /**
     * 获取运单分页数据
     *
     * @param dto
     * @return
     */
    @ApiOperation(value = "获取运单分页数据", notes = "订单Id-运单状态-运单调度状态")
    @GetMapping("/page")
    public R<TransportOrder> page(TransportOrderDTO dto) {
        IPage<TransportOrder> orderIPage = service.findByPage(dto);

        return new R(ResultCode.SUCCESS_CODE, "获取运单分页数据成功!", orderIPage);
    }

    /**
     * 根据id获取运单信息
     *
     * @param id 运单id
     * @return 运单信息
     */
    @ApiOperation(value = "根据id获取运单信息", notes = "运单id")
    @GetMapping("/{id}")
    public R<TransportOrder> findById(@PathVariable(name = "id") String id) {
        TransportOrder transportOrder = service.getById(id);

        return new R(ResultCode.SUCCESS_CODE, "根据id获取运单信息成功!", transportOrder);
    }

    /**
     * 根据订单id获取运单信息
     *
     * @param orderId 订单id
     * @return 运单信息
     */
    @ApiOperation(value = "根据订单id获取运单信息", notes = "顶单id")
    @GetMapping("/{orderId}")
    public R<TransportOrder> findByOrderId(@PathVariable(name = "orderId") String orderId) {
        TransportOrder transportOrder = service.findByOrderId(orderId);
        return new R(ResultCode.SUCCESS_CODE, "根据订单id获取运单信息成功!", transportOrder);
    }

    /**
     * 根据多个条件获取运单信息
     *
     * @param dto 订单id
     * @return 运单信息
     */
    @ApiOperation(value = "根据多个条件获取运单信息", notes = "必须:orderIds-可选:运单状态-运单调度状态")
    @GetMapping("/list")
    public R<TransportOrder> list(TransportOrderDTO dto) {
        List<TransportOrder> tos = service.findByOrderIds(dto);
        return new R(ResultCode.SUCCESS_CODE, "根据多个条件获取运单信息成功!", tos);
    }


    /**
     * 根据订单id查找它的运单信息
     *
     * @param orderId
     * @return
     */

    @ApiOperation("根据订单id查找它的运单信息")
    @GetMapping("/getByOrderId/{orderId}")
    public R<TransportOrder> getByOrderId(@PathVariable String orderId) {
        if (StringUtils.isBlank(orderId)) {
            throw new TmsException(ResultCode.ERROR_CODE, "参数异常!");
        }
        QueryWrapper<TransportOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", orderId);
        TransportOrder order = service.getOne(queryWrapper);
        return new R<>(ResultCode.SUCCESS_CODE, order);
    }

    /**
     * 将运单改为已装车和已调度状态
     *
     * @param orderId
     * @return
     */
    @GetMapping("/modifyStatusDispatch/{orderId}")
    public R<TransportOrder> modifyStatusDispatch(@PathVariable String orderId) {
        QueryWrapper<TransportOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", orderId);
        //查询运单对象数据
        TransportOrder order = service.getOne(queryWrapper);
        TransportOrder transportOrder = new TransportOrder();
        transportOrder.setStatus(2);
        transportOrder.setSchedulingStatus(3);
        transportOrder.setOrderId(orderId);
        transportOrder.setId(order.getId());
        service.update(transportOrder, queryWrapper);
        return new R<>(ResultCode.SUCCESS_CODE, transportOrder);
    }

    /**
     * 将运单改为未匹配线路状态
     *
     * @param orderId
     * @return
     */
    @GetMapping("/modifyStatusNotMatched/{orderId}")
    public R<TransportOrder> modifyStatusNotMatched(@PathVariable String orderId) {
        QueryWrapper<TransportOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", orderId);
        TransportOrder transportOrder = new TransportOrder();
        transportOrder.setOrderId(orderId);
        transportOrder.setSchedulingStatus(2);
        service.update(transportOrder, queryWrapper);
        return new R<>(ResultCode.SUCCESS_CODE, transportOrder);
    }


    /**
     * 根据运单Id查询运单详情
     *
     * @param transportOrderId
     * @return
     */
    @ApiOperation("根据运单Id查询运单详情")
    @GetMapping("/findTransportOrderDetailsById/{transportOrderId}")
    public R<TransportOrderDTO> findTransportOrderDetailsById(@PathVariable String transportOrderId) {
        R<TransportOrderDTO> r = service.findTransportOrderDetailsById(transportOrderId);
        return r;
    }


    /**
     * 根据订单ID查询其对应的线路,车次,车辆,司机数据
     *
     * @param orderId 订单Id
     * @return
     */
    @ApiOperation("根据订单ID查询其对应的线路,车次,车辆,司机数据")
    @GetMapping("/findTransportOrderDetailsByOrderById/{orderId}")
    public R<TransportOrderDetailsVO> findTransportOrderDetailsByOrderById(@PathVariable String orderId) {
        R<TransportOrderDetailsVO> r = service.findTransportOrderDetailsByOrderById(orderId);
        return r;
    }

    /**
     * 获取运单状态数量
     * @return
     */
    @GetMapping("/getTransportOrderCount")
    @ApiOperation("获取运单状态数量")
    public R<TransportOrderDashboardVO> getTransportOrderCount() {
        R<TransportOrderDashboardVO> r = service.getTransportOrderCount();
        return r;
    }

}

